<template>
	<view class="goods_list">
		<view class="listTree">
			<scroll-view style="white-space: nowrap;" scroll-x="true">
				<text v-for="(item, index) in categories" :class="activeClass === index ? 'active_style' : 'cate_style'" @click="changeCategory(item.id, index)">{{ item.name }}</text>
			</scroll-view>
		</view>
		<view class="listContent">
			<!-- 父组件中响应子组件的自定义事件@goodsItemClick -->
			<goods-list @goodsItemClick="goGoodsDetail" :goods="goods"></goods-list>
		</view>
		<!-- <view class="isOver" v-if="flag">-----我是有底线的-----</view> -->
	</view>
</template>

<script>
	import goodsList from '../../components/goods-list/goods-list.vue'
	export default {
		data() {
			return {
				pageindex: 1,
				goods: [],
				flag: false,
				categories: [],
				activeClass: 0
			}
		},
		components: {
			"goods-list": goodsList
		},
		methods: {
			async getProductCategory() {
				const res = await this.$myRequest({
					url: '/api/category/list'
				})
				this.categories = res.data.data
			},
			// 获取商品列表数据
			async getGoodsList(callBack) {
				const res = await this.$myRequest({
					url: '/api/product/list'
				})
				// this.goods = [...this.goods, ...res.data.data]
				this.goods = res.data.data
				console.log(this.goods)
				// callBack && callBack()
			},
			// 导航到商品详情页,参数id是商品的productId
			goGoodsDetail(id) {
				uni.navigateTo({
					url: '/pages/goods-detail/goods-detail?id=' + id
				})
			},
			//id是商品的id，idx是scroll-view中某个类别所处的序号
			async changeCategory(id, idx) {
				const res = await this.$myRequest({
					url: '/api/product/list?categoryId=' + id
				})
				this.goods = res.data.data
				console.log('@@@', this.goods)
				//将活动元素设置为选中的那个元素
				this.activeClass = idx
			}
		},
		onLoad() {
			this.getGoodsList()
			this.getProductCategory()
		},
		onReachBottom() {
			if (this.goods.length < this.pageindex * 10) return this.flag = true
			this.pageindex++
			this.getGoodsList()
		},
		onPullDownRefresh() {
			console.log('下拉刷新了')
			this.pageindex = 1
			this.goods = []
			this.flag = false
			setTimeout(() => {
				this.getGoodsList(() => {
					uni.stopPullDownRefresh()
				})
			}, 1000)
		}
	}
</script>

<style lang="scss">
	.goods_list {
		background-color: #eee;

		.listTree {
			width: 100%;
			background-color: #efe;
			/* 预留状态栏区域 */
			position: fixed;
			// top: 0;
			left: 0;
			right: 0;
			z-index: 999;
			// padding-top: var(--status-bar-height);
		}

		::v-deep {
			.active_style {
				display: inline-block;
				height: 30px;
				font-size: 16px;
				margin: 5rpx 10px;
				color: red !important;
			}
			.cate_style {
				display: inline-block;
				height: 30px;
				font-size: 16px;
				margin: 5rpx 10px;
				color: #333;
			}
		}
		.listContent {
			width: 100%;
			height: 100%;
			padding-top: 30px; //此处改为margin-top则会出错
		}
	}

	.isOver {
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		// background: #fff;
		font-size: 28rpx;
	}
</style>
